package com.njupt.yanglonglong.report.demo.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.njupt.yanglonglong.report.demo.dto.CourseTeacherInfoDto;
import com.njupt.yanglonglong.report.demo.entity.Course;
import com.njupt.yanglonglong.report.demo.entity.RCourseTeacher;
import com.njupt.yanglonglong.report.demo.mapper.RCourseTeacherMapper;
import com.njupt.yanglonglong.report.demo.service.RCourseTeacherService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @className: RCourseTeacherImpl
 * @Description:
 * @author: lpl
 * @date: 2021/4/27 22:15
 */
@Service
@Slf4j
public class RCourseTeacherImpl extends ServiceImpl<RCourseTeacherMapper, RCourseTeacher> implements RCourseTeacherService {

    @Autowired
    RCourseTeacherMapper rCourseTeacherMapper;

    @Override
    public int deleteByTCId(RCourseTeacher rCourseTeacher) {
        QueryWrapper<RCourseTeacher> qw = new QueryWrapper<>();
        qw.eq("teacher_id", rCourseTeacher.getTeacher_id()).eq("course_id", rCourseTeacher.getCourse_id());
        return rCourseTeacherMapper.delete(qw);
    }

    @Override
    public List<Course> getTeacherCourse(String teacherId) {
        return rCourseTeacherMapper.getAllByTeacherId(teacherId);
    }

    @Override
    public List<CourseTeacherInfoDto> queryOpenedCourses(String stuId) {
        return rCourseTeacherMapper.queryOpenedCourses(stuId);
    }

    @Override
    public boolean updateByCourseIdTeacherId(RCourseTeacher rCourseTeacher) {
        QueryWrapper<RCourseTeacher> rCourseTeacherQueryWrapper = new QueryWrapper<>();
        rCourseTeacherQueryWrapper.eq("course_id", rCourseTeacher.getCourse_id())
                .eq("teacher_id", rCourseTeacher.getTeacher_id());
        RCourseTeacher tmp = new RCourseTeacher();
        tmp.setEndTime(rCourseTeacher.getEndTime());
        tmp.setStartTime(rCourseTeacher.getStartTime());
        int update = rCourseTeacherMapper.update(tmp, rCourseTeacherQueryWrapper);
        tmp = null;
        log.info("updateByCourseIdTeacherId更改了" + update + "条数据");
        return update > 0;
    }

}
